/*
 * @Author: chunlai
 * @Date: 2025-05-27 21:12:23
 * @LastEditTime: 2025-08-21 11:54:19
 * @LastEditors: minsheng_insurance_agency_zhoucl
 * @FilePath: \02.bldinsure\src\mixins\pages.js
 * @Description: 页面链接设置
 */

import { mapGetters } from "vuex";
import { wxPageShare } from "@/utils/wxJsSdk";
import { filterFieldValue, formatUrlSearch } from "@/methods/format.js";
export const pagesMixin = {
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      shareInfo: "home/shareInfo",
      productName: "varietyInfo/productName",
    }),
  },
  created() {},
  methods: {
    setWxShareConfig(to) {
      let uqrys = formatUrlSearch(),
        desc = "请协助完善投保信息",
        title = `${this.productName}`;
      if (to?.meta?.title) {
        desc = to.meta.title;
      }
      if (uqrys.indexOf("ptype") != -1) {
        uqrys = filterFieldValue(uqrys, "ptype");
      }
      if (uqrys.indexOf("wxshare") != -1) {
        uqrys = filterFieldValue(uqrys, "wxshare");
      }
      if (uqrys.indexOf("stamp") != -1) {
        uqrys = filterFieldValue(uqrys, "stamp");
      }
      const domainVal = window.location.origin + window.location.pathname,
        pname = to.path.replace("/", "");
      let baseLink = `${domainVal}${uqrys}`;
      if (!this.$getDataFromUrl("orderNum")) {
        // 新单
        if (this.$getMessage("saveOrder") === "1") {
          baseLink += `&orderNum=${this.orderNum}&ptype=${pname}`;
        }
      } else {
        baseLink += `&ptype=${pname}`;
      }
      // console.log(`baseLink:${baseLink}`);
      wxPageShare(
        `${baseLink}&stamp=${new Date().getTime()}`,
        `${title}`,
        `${desc}`
      );
    },
  },
};

export const rrwebMixin = {
  data() {
    return {
      rrwebFn: "",
      events: [],
      emitTime: 0,
    };
  },
  computed: {},
  methods: {
    openRrweb() {
      if (process.env.NODE_ENV === "development") return;
      if (process.env.VUE_APP_PRODFLAG === "wxsc") return;
      let that = this;
      this.rrwebFn = rrwebRecord({
        emit(event) {
          // console.log("Captured event:", event);
          that.events.push(event);
          if (that.events.length > 10) {
            that.sendDatas(that.events);
          }
          const now = new Date().getTime();
          if (that.emitTime && now - that.emitTime > 1000) {
            that.sendDatas(that.events);
          }
          if (!that.rrwebFn) {
            that.sendDatas(that.events);
          }
        },
        blockClass: "rrweb-ignore",
      });
    },
    sendDatas(datas = []) {
      // console.log('----1', datas);
      if (datas.length === 0) return;
      const dtime = datas[0] && datas[0].timestamp;
      let params = {
        workNum: this.$getDataFromUrl("workNum"),
        orderNum: this.orderNum,
        timeOrder: dtime,
        kind: this.rrwebType || "20", // 回溯视屏类型
        unionId: "", // 微信端
        info: JSON.stringify(datas),
      };
      if (!params.workNum || !params.orderNum) return;
      this.events = [];
      this.emitTime = new Date().getTime();
      this.$http
        .post("https://wxou.mschn.cn/msmicro/backtrack/v1/save", params, {
          showDialog: false,
        })
        .then((res) => {
          // console.log(res);
        })
        .catch((error) => {
          // console.log(error);
        });
    },
  },
};
